#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <windows.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tcpSocket = NULL;

    //分配空间，指定父对象
    tcpSocket = new QTcpSocket(this);

    startX=this->pos().x();
    startY=this->pos().y();
    showW = this->width();
    showH = this->height();

    lastX=0;
    lastY=0;

    mode=false;
    subMode=false;
    ownX=0,ownY=0;
    recvLastX=-1,recvLastY=-1;
    isOpen=false;

    ui->btn_4->setVisible(false);
    
    connect(tcpSocket,&QTcpSocket::connected,[=](){
        ui->btn_2->setText("OK");
        isOpen=true;
    });

    recvLen=0;
    serverWidth=1080;
    serverHight=1920;
    connect(tcpSocket,&QTcpSocket::readyRead,[=](){
        QByteArray buf = tcpSocket->readAll();
        char *temp = buf.data();
//        ui->btn_2->setText(QString::number(buf.size()));
        for(int i=0;i<buf.size();i++)
        {
            recvBuf[recvLen]=temp[i];
            recvLen++;
            if(recvLen==64)
            {
                char data[64];
                for(int n=0;n<64;n++)
                    data[n]=recvBuf[n];
                recvLen=0;
                server_recv(data);
            }

        }

    });
}

MainWindow::~MainWindow()
{
    display.close();
    delete ui;
}


void MainWindow::setMouse(int x, int y, int type)
{
    double rateW;
    double rateH;
    int width=this->width();
    int height=this->height();
    int tx=0;
    int ty=0;

    if(mode)
        x=ownX,y=ownY;

    if(width>=height)//横的
    {
        rateW=1.0*width/serverHight;    //宽边对应于手机的高
        rateH=1.0*height/serverWidth;
        tx=y*rateW;
        ty=height-x*rateH;
    }
    else    //竖着的
    {
        rateW=1.0*width/serverWidth;    //宽边对应于手机的宽
        rateH=1.0*height/serverHight;
        tx=x*rateW;
        ty=y*rateH;
    }

    ::SetCursorPos(startX+tx,startY+ty);

    if(type==MOUSE_RIGHTCLICK)
    {
        mouse_event(MOUSEEVENTF_RIGHTDOWN, startX+tx, startY+ty,0,0);
        mouse_event(MOUSEEVENTF_RIGHTUP, startX+tx, startY+ty,0,0);//
    }
    else if(type==MOUSEDOWN)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, startX+tx, startY+ty,0,0);
    }
    else if(type==MOUSERELEASE)
    {
        mouse_event(MOUSEEVENTF_LEFTUP, startX+tx, startY+ty,0,0);
    }
    else if(type==MOUSE_DCLICK)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, startX+tx, startY+ty,0,0);
        mouse_event(MOUSEEVENTF_LEFTUP, startX+tx, startY+ty,0,0);
        Sleep(10);
        mouse_event(MOUSEEVENTF_LEFTDOWN, startX+tx, startY+ty,0,0);
        mouse_event(MOUSEEVENTF_LEFTUP, startX+tx, startY+ty,0,0);
    }
}

void MainWindow::mouseRelaseProc(int x, int y)
{
    setBtnPos(x,y); // ui

    QTime stopTime = QTime::currentTime();
    int elapsed = startTime.msecsTo(stopTime);//ms
    
    if(mouseState==MOUSEDOWN&&x==lastX&&y==lastY)
    {
        if(elapsed>1000)
        {
//            cout <<"LongPress times:"<<elapsed<<endl;
            setMouse(x,y,MOUSE_RIGHTCLICK);
        }
        else
        {
//            cout <<"SignleClick times:"<<elapsed<<endl;
            setMouse(x,y,MOUSEDOWN);
            setMouse(x,y,MOUSERELEASE);
        }
    }
    else if(elapsed>1000&&mouseState==MOUSE_DCLICK&&x==lastX&&y==lastY)
    {
//        cout <<"DoubleClick times:"<<elapsed<<endl;
        setMouse(x,y,MOUSE_DCLICK);
    }
    else
    {
        setMouse(x,y,MOUSERELEASE);
    }

    //update
    mouseState=MOUSERELEASE;
    lastX=x,lastY=y;
}

void MainWindow::server_recv(char recvBuf[])
{
    int x=0,y=0;
    if(cmp_str(recvBuf,"mouseMove"))
    {
        getPos(recvBuf,x,y);
//        cout <<"mouseMove: "<<x<<" "<<y<<endl;
        if(mode)
        {
            int ax=x-recvLastX;
            int ay=y-recvLastY;
            ownX+=ax;
            ownY+=ay;
            recvLastX=x;
            recvLastY=y;
            if(ownX<0) ownX=0;
            if(ownX>serverWidth) ownX=serverWidth;
            if(ownY<0) ownY=0;
            if(ownY>serverHight) ownY=serverHight;
        }
        setBtnPos(x,y);
        setMouse(x,y,MOUSEMOVE);
    }
    else if(cmp_str(recvBuf,"mouseClick"))
    {
        getPos(recvBuf,x,y);
//        cout <<"mouseClick: "<<x<<" "<<y<<endl;
        setBtnPos(x,y);
        if(subMode)
            setMouse(x,y,MOUSEDOWN);
        if(mode)
        {
            recvLastX=x;
            recvLastY=y;
        }

        lastX=x,lastY=y;
        startTime = QTime::currentTime();
        mouseState=MOUSEDOWN;
    }
    else if(cmp_str(recvBuf,"mouseRelease"))
    {
        getPos(recvBuf,x,y);
//        cout <<"mouseRelease: "<<x<<" "<<y<<endl;
        mouseRelaseProc(x,y);
    }
    else if(cmp_str(recvBuf,"mouseDClick"))
    {
        getPos(recvBuf,x,y);
//        cout <<"mouseDClick: "<<x<<" "<<y<<endl;
        setBtnPos(x,y);
        setMouse(x,y,MOUSEDOWN);
        lastX=x,lastY=y;
        mouseState=MOUSE_DCLICK;
    }
    else if(cmp_str(recvBuf,"information"))
    {
        getPos(recvBuf,x,y);
//        cout <<"information: "<<x<<" "<<y<<endl;
        serverWidth=x;
        serverHight=y;
    }
    else if(cmp_str(recvBuf,"step"))
    {
        getPos(recvBuf,x,y);
//        cout <<"information: "<<x<<" "<<y<<endl;
        display.close();
        display.init(startX,startY,showW,showH,x,y);
    }
    else
    {
        cout <<recvBuf<<endl;
    }
}

void MainWindow::on_btn_clicked()
{
    //display.init(startX,startY,showW,showH,20,40);
}

void MainWindow::on_btn_2_clicked()
{
    if(!isOpen)
    {
        QHostAddress test;
        QString ip=ui->inputIP->text();
        if (test.setAddress(ip))
            tcpSocket->connectToHost(QHostAddress(ip),8111);
    }
}

void MainWindow::getPos(char src[], int &x, int &y)
{
    int c=0,i=0;
    int t=0;
    for(i=0;i<strlen(src);i++)
    {
        if(src[i]==' ')
        {
            break;
        }
    }
    sscanf(&src[i+1],"%d %d",&x,&y);
}


bool MainWindow::cmp_str(const char src[], const char dst[])
{
    int l=strlen(dst);
    if(l>strlen(src))
        return false;
    for(int i=0;i<l;i++)
    {
        if(src[i]!=dst[i])
            return false;
    }
    return true;
}

void MainWindow::setBtnPos(int x, int y)
{
    double rateW;
    double rateH;
    int width=this->width();
    int height=this->height();
    if(width>=height)//横的
    {
        rateW=1.0*width/serverHight;    //宽边对应于手机的高
        rateH=1.0*height/serverWidth;
        ui->btn->setGeometry(y*rateW,this->height()-x*rateH,30,30);
    }
    else    //竖着的
    {
        rateW=1.0*width/serverWidth;    //宽边对应于手机的宽
        rateH=1.0*height/serverHight;
        ui->btn->setGeometry(x*rateW,y*rateH,30,30);
    }
}

void MainWindow::on_btn_3_clicked()
{
    startX=this->pos().x();
    startY=this->pos().y();
    showW = this->width();
    showH = this->height();
    cout <<"here: "<<startX<<" "<<startY<<endl;
    display.init(startX,startY,showW,showH,20,40);
    ui->btn_4->setVisible(true);
    netShow=true;
    ui->btn_4->setText(tr("CloseNet"));
}

void MainWindow::on_btn_4_clicked()
{
    if(netShow)
    {
        ui->btn_4->setText(tr("OpenNet"));
        display.close();
        netShow=false;
    }
    else{
        ui->btn_4->setText(tr("CloseNet"));
        display.show();
        netShow=true;
    }
}

void MainWindow::on_btn_5_clicked()
{
    if(mode)
    {
        ui->btn_5->setText(tr("Mode0"));
        ui->btn_6->setVisible(true);
        ui->btn_6->setText(tr("0"));
        subMode=false;
        mode=false;
    }
    else
    {
        ui->btn_5->setText(tr("Mode1"));
        ui->btn_6->setVisible(false);
        subMode=false;
        mode=true;
    }
}

void MainWindow::on_btn_6_clicked()
{
    if(subMode){
        ui->btn_6->setText(tr("0"));
        subMode=false;
    }
    else{
        ui->btn_6->setText(tr("1"));
        subMode=true;
    }
}
